//
//  CompanyRegSetp1ViewController.m
//  CarTown
//
//  Created by 熊浩 on 15/7/31.
//  Copyright (c) 2015年 熊浩. All rights reserved.
//

#import "CompanyRegSetp1ViewController.h"
#import "CompanyRegStep2ViewController.h"
#import "LCActionSheet.h"
#import "SDPhotoBrowser.h"
#import <MBProgressHUD.h>
#import "FaRenViewController.h"
#import "WeiTuoViewController.h"
#import "ShengfenShowViewController.h"

@interface CompanyRegSetp1ViewController ()<LCActionSheetDelegate,UINavigationControllerDelegate, UIImagePickerControllerDelegate,SDPhotoBrowserDelegate>
@property (weak, nonatomic) IBOutlet UITextField *c_username;
@property (weak, nonatomic) IBOutlet UITextField *c_name;
@property (weak, nonatomic) IBOutlet UITextField *c_pass;
@property (weak, nonatomic) IBOutlet UITextField *c_code;
@property (weak, nonatomic) IBOutlet UIButton *yingye;
@property (weak, nonatomic) IBOutlet UIButton *shouzhi;
@property (weak, nonatomic) IBOutlet UIButton *follow;
@property (weak, nonatomic) IBOutlet UIButton *back;

@property (weak, nonatomic) IBOutlet UIView *photobgview;
@property (nonatomic,strong) NSArray *btnarray;

@property (nonatomic,strong) NSMutableArray *imgarray;
@property (nonatomic,strong) UIImagePickerController *picker;
@property (nonatomic,strong) UUImageAvatarBrowser *brower;
@end

@implementation CompanyRegSetp1ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=@"公司注册";
    
    
    // Do any additional setup after loading the view.
}
-(NSMutableArray *)imgarray{
    if (_imgarray==nil) {
        _imgarray=[NSMutableArray array];
    }
    return _imgarray;
}

-(NSArray *)btnarray{
    if (_btnarray==nil) {
        _btnarray=@[self.yingye,self.back,self.follow,self.shouzhi];
    }
    return _btnarray;
}
- (IBAction)photoex_click:(id)sender {
}
- (IBAction)notsafeclick:(id)sender {
    self.c_pass.secureTextEntry=!self.c_pass.secureTextEntry;
}
-(BOOL)checkForm{
    if (self.c_username.text.length==0) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"公司账号不能为空";
        hud.mode = MBProgressHUDModeText;
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1];
        return false;
    }
    if (self.c_name.text.length==0) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"公司名称不能为空";
        hud.mode = MBProgressHUDModeText;
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1];
        return false;
    }
    if (self.c_pass.text.length==0||self.c_code.text.length<6) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"密码必须大于等于6位";
        hud.mode = MBProgressHUDModeText;
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1];
        return false;
    }
    if (self.c_code.text.length==0) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"营业执照不能为空";
        hud.mode = MBProgressHUDModeText;
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1];
        return false;
    }
    if (!self.yingye.isSelected) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"营业执照照片不能为空";
        hud.mode = MBProgressHUDModeText;
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1];
        return false;
    }
    if (!self.back.isSelected) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"身份证背面照片不能为空";
        hud.mode = MBProgressHUDModeText;
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1];
        return false;
    }
    if (!self.follow.isSelected) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"身份证正面照片不能为空";
        hud.mode = MBProgressHUDModeText;
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1];
        return false;
    }
    if (!self.shouzhi.isSelected) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"手持身份证照片不能为空";
        hud.mode = MBProgressHUDModeText;
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1];
        return false;
    }
    return true;
}

- (IBAction)yingyeclick:(UIButton *)sender {
    if (sender.isSelected) {
        [self showHasActionSheet:sender.tag];
    }else{
        [self showActionSheet:sender.tag];
    }
    
}

- (IBAction)shenfenzex_clcik:(id)sender {
}

-(void)showActionSheet:(NSInteger)tag{
    
    LCActionSheet *sheet=[[LCActionSheet alloc]initWithTitle:nil buttonTitles:@[@"拍照",@"从相册中选取"] redButtonIndex:-1 delegate:self];
    sheet.tag=tag;
    sheet.isTag=NO;
    [sheet show];
}
-(void)showHasActionSheet:(NSInteger)tag{
    
    LCActionSheet *sheet=[[LCActionSheet alloc]initWithTitle:nil buttonTitles:@[@"查看大图",@"重新上传",@"删除"] redButtonIndex:2 delegate:self];
    sheet.tag=tag;
    sheet.isTag=YES;
    [sheet show];
}
-(void)showCarmera:(NSInteger)tag{
    UIImagePickerControllerSourceType sourcheType = UIImagePickerControllerSourceTypeCamera;
   self.picker.view.tag=tag;
    self.picker.sourceType=sourcheType;
    [self presentViewController:self.picker animated:YES completion:nil];
}
-(void)showImagePicker:(NSInteger)tag{
     UIImagePickerControllerSourceType sourcheType = UIImagePickerControllerSourceTypePhotoLibrary;
    self.picker.sourceType=sourcheType;
    self.picker.view.tag=tag;
    [self presentViewController:self.picker animated:YES completion:nil];
}

-(UIImagePickerController *)picker{
    if (_picker==nil) {
        _picker=[[UIImagePickerController alloc]init];
       _picker.view.backgroundColor = [UIColor orangeColor];
       
        _picker.delegate =self;
    }
    return _picker;
}

-(void)switchImagePickerOrActionsheet:(BOOL)ishasimg WithTag:(NSInteger)tag index:(NSInteger)buttonindex{
        if (buttonindex==0) {
            [self showCarmera:tag];
        }else{
            [self showImagePicker:tag];
        }
    
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    [self dismissViewControllerAnimated:YES completion:^{
        UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
        
        NSLog(@"%f,%f",image.size.width,image.size.height);
        
        UIButton *btn=self.btnarray[picker.view.tag];
        btn.selected=YES;
        [btn
         setImage:image forState:UIControlStateSelected];
    }];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)fanliclick:(UIButton *)sender {
    
    switch (sender.tag) {
        case 0:
        {
            WeiTuoViewController *vc=[[WeiTuoViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 1:
        {
            ShengfenShowViewController*vc=[[ShengfenShowViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 2:
        {
            FaRenViewController *vc=[[FaRenViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        default:
            break;
    }

    
    
}
-(void)actionSheet:(LCActionSheet *)actionSheet didClickedButtonAtIndex:(NSInteger)buttonIndex{
    NSLog(@"%ld---index--%ld",actionSheet.tag,buttonIndex);
     
    if (buttonIndex==actionSheet.btnCount-1) return;
    if (actionSheet.isTag) {
        switch (buttonIndex) {
            case 0:
                [self showimage:actionSheet.tag];

                break;
            case 1:
                [self showActionSheet:actionSheet.tag];
                break;
            case 2:
            {
                
                UIButton *btn=self.btnarray[actionSheet.tag];
                btn.selected=NO;
                [btn setImage:nil forState:UIControlStateSelected];
            }
                break;
            default:
                break;
        }
        
      
    }else{
        UIButton *btn=self.btnarray[actionSheet.tag];
        [self switchImagePickerOrActionsheet:btn.isSelected WithTag:actionSheet.tag index:buttonIndex];
    }
}

-(UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index{
    UIButton *btn=self.btnarray[browser.tag];
    return btn.imageView.image;
}

-(void)showimage:(NSInteger)index{
    SDPhotoBrowser *browser=[[SDPhotoBrowser alloc]init];
    browser.delegate=self;
    browser.currentImageIndex=0;
    browser.imageCount=1;
    UIButton *btn=self.btnarray[index];
    browser.sourceImagesContainerView=btn;
    browser.tag=index;
    [browser show];
}

-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)nextclick:(id)sender {
   if ([self checkForm]) {
        CompanyRegStep2ViewController *vc=[CompanyRegStep2ViewController viewcontroller];
        vc.cname=self.c_name.text;
        vc.cusername=self.c_username.text;
        vc.cpassword=self.c_pass.text;
        vc.ccode=self.c_code.text;
        vc.yingyeimg=[self.yingye currentImage];
        vc.backimg=[self.back currentImage];
        vc.shouchiimg=[self.shouzhi currentImage];
        vc.followimg=[self.follow currentImage];
        [self.navigationController pushViewController:vc animated:YES];

    }
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
